﻿@using FwProject.EventAdvertising.Dto
@using FwProject.ShoppingMall.EventAdvertising
@model ShopCouponInput
@{
}
<style>
    .hidden {
        display: none;
    }
</style>
<form id="form0" class="layui-form" action="@Url.Action("Create")" method="post" novalidate="novalidate" data-ajax="true" data-ajax-begin="LockAjaxCallbackBack" data-ajax-success="ReloadAjaxCallbackgoback" data-ajax-failure="FailureAjaxCallback" data-ajax-method="Post" role="form" lay-filter="form1">
    <div class="layui-form layui-form-pane" style="padding:10px;">
        <div class="layui-form-item">
            @Html.LabelFor(o => o.CouponTitile, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @Html.TextBoxFor(o => o.CouponTitile, new { @class = "layui-input" })
                @Html.ValidationMessageFor(o => o.CouponTitile)
            </div>
        </div>
        <div class="layui-form-item">
            @Html.LabelFor(o => o.CouponPrice, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @Html.TextBoxFor(o => o.CouponPrice, new { @class = "layui-input" })
                @Html.ValidationMessageFor(o => o.CouponPrice)
            </div>
        </div>
        <div class="layui-form-item">
            @Html.LabelFor(o => o.CouponType, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                <enums asp-name="CouponType" asp-id="CouponType" asp-valuetype="2" lay-filter="couponType" asp-enum="typeof(CouponType)"></enums>
            </div>
        </div>
        <div class="layui-form-item hidden" id="ptype">
            <label class="layui-form-label">商品类别</label>
            <div class="layui-input-inline">
                @Html.DropDownList("P", ViewBag.Parent as IEnumerable<SelectListItem>, "请选择上级商品分类", new { @lay_filter = "parenttype" })
            </div>
            <div class="layui-input-inline" lay-filter="productType">
                <select name="ProductTypeId" id="ProductTypeId">
                    <option value="">请选择分类</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" >
            @Html.LabelFor(o => o.UseCoupon, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                <enums asp-name="UseCoupon" asp-valuetype="2" lay-filter="useCoupon" asp-enum="typeof(UseCoupon)"></enums>
            </div>
        </div>
        <div class="layui-form-item" id="mj" style="display:none;">
            @Html.LabelFor(o => o.UsePrice, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @*@Html.TextBoxFor(o => o.UsePrice, new { @class = "layui-input" })*@
                <input type="text" name="UsePrice" id="UsePrice" class="layui-input" value="0"/>
            </div>
        </div>

        <div class="layui-form-item layui-upload">
            @Html.LabelFor(o => o.CouponPic, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" id="up_btn" data-src="#imagePic" style="float:left;">选择图片</button>
                <img class="layui-upload-img" id="imagePic" style="float:left; padding-left:10px;">
                @Html.HiddenFor(o => o.CouponPic)
                @Html.ValidationMessageFor(o => o.CouponPic)
            </div>
        </div>
        <div class="layui-form-item">
            @Html.LabelFor(o => o.UseBeginTime, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @Html.TextBoxFor(o => o.UseBeginTime, new { @class = "layui-input" })
                @Html.ValidationMessageFor(o => o.UseBeginTime)
            </div>
        </div>
        <div class="layui-form-item">
            @Html.LabelFor(o => o.UseEndTime, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @Html.TextBoxFor(o => o.UseEndTime, new { @class = "layui-input" })
                @Html.ValidationMessageFor(o => o.UseEndTime)
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="margin-bottom: 100px;">
            @Html.LabelFor(o => o.CouponDescribe, new { @class = "layui-form-label" })
            <div class="layui-input-block">
                @Html.TextAreaFor(o => o.CouponDescribe, new { @class = "layui-textarea" })
                @Html.ValidationMessageFor(o => o.CouponDescribe)
            </div>
        </div>
        <div class="layui-layer-btn layui-layer-btn-" style="padding:8px 12px;z-index:1000;position: fixed;bottom:0;left: 1px;width: 98%;height: 50px; background:rgba(230,230,230,0.8)">
            <a id="save" class="layui-layer-btn0" lay-submit="" lay-filter="saveprops">保存</a>
        </div>
    </div>
</form>

@section scripts{
    <script src="~/js/main.js"></script>
    @*<script src="~/Theme/js/plugins/layer/laydate/laydate.js"></script>*@
    <script src="~/js/laydate/laydate.js"></script>
    <script type="text/javascript">
        var form, layer, layedit, upload;
        layui.use(['form', 'layedit', 'layer','upload'], function () {
            layedit = layui.layedit;
            form = layui.form;
            layer = layui.layer;
            upload = layui.upload;

            //自定义验证规则
            form.verify({
                required: function (value) {
                    if (value == "") {
                        return '必填项不能为空';
                    }
                }
            });


            upload.render({
                elem: '#up_btn'
                , url: '@Url.Action("UpLoadImg")'
                , multiple: false//多文件上传
                , auto: true//自动上传
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#imagePic').attr('src', result)
                        //$('#preview2').append('<img src="' + result + '" alt="' + file.name + '" class="layui-upload-img">')
                    });
                }
                , done: function (res) {
                    $('#CouponPic').val(res.result)
                    //上传完毕
                }
            });

            //使用类别监听
            form.on('select(couponType)', function (data) {
                if (data.value =="Single") {
                    $("#ptype").show();
                    $("#ProductTypeId").attr("lay-verify", "required");//添加验证
                } else {
                    $("#ptype").hide();
                    $("#ProductTypeId").removeAttr("lay-verify");
                }
            });

            //适用范围监听
            form.on('select(useCoupon)', function (data) {
                if (data.value == "FullReduction") {
                    $("#UsePrice").attr("lay-verify","required");
                } else {
                    $("#UsePrice").removeAttr("lay-verify");
                }

            });

            //商品类别监听
            form.on('select(parenttype)', function (data) {
                $("#ProductTypeId").find("option").remove();
                if (data.value!="") {
                    $.get("/ShopType/GetChildTypeData?parentid=" + data.value, function (data) {
                        if (data.success) {
                            if (data.result.length>0) {
                                for (var i = 0; i < data.result.length; i++) {
                                    var option = $("<option>").val(data.result[i].id).text(data.result[i].typeName);
                                    $("#ProductTypeId").append(option);

                                }
                                form.render('select');
                            }
                        }
                    })
                }
            });

            //商品类别监听
            form.on('select(useCoupon)', function (data) {
                if (data.value =="FullReduction") {
                    $("#mj").show();
                } else {
                    $("#mj").hide();
                }
            });
        })

        function ReloadAjaxCallbackgoback(data, successInfo) {
            if (data.success) {
                layer.msg('保存成功！')
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭当前iframe页面
            } else {
                layer.msg('保存失败：' + successInfo);
            }
        }

        $('#save').on('click', function () {
            $('#form0').submit();
        });

        $(document).ready(function () {
            var start = {
                elem: "#UseBeginTime",
                format: "YYYY/MM/DD hh:mm:ss",
                min: "1980-01-01 00:00:00",
                max: "2099-06-16 23:59:59",
                istime: true,
                istoday: false,
                choose: function (datas) {
                    end.min = datas;
                    end.start = datas
                }
            };
            var end = {
                elem: "#UseEndTime",
                format: "YYYY/MM/DD hh:mm:ss",
                min: laydate.now(),
                max: "2099-06-16 23:59:59",
                istime: true,
                istoday: false,
                choose: function (datas) {
                    start.max = datas
                }
            };

            laydate(start);
            laydate(end);


        });
    </script>
}

